/*

ClassFinder.as v1.1.0

Copyright (c) 2006 Steve Webster

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

class utils.ClassFinder {
	
	private static var classNameCache:Array = new Array();
	
	public static function find(package:String) : Function {
		var packageParts:Array = package.split(".")
		var obj:Object = _global;
		
		for (var i:Number = 0; i < packageParts.length; i++) {
			obj = obj[packageParts[i]];
		}
		
		return Function(obj);
	}
	
	public static function getClassName(object:Object, full:Boolean) : String {
		var classObject:Object = object instanceof Function ? object : object.__constructor__;
		
		trace(classObject);
		
		var fullClassName:String
		
		// Check cache for classname
		if (classObject.__df_className.length > 0) {
			fullClassName = classObject.__df_className;
			
		} else {
			// Make global tree entirely readable
			_global.ASSetPropFlags(_global, null, 0, true);

			// Lookup class name
			fullClassName = searchForClass(classObject, _global);
			
			// Cache class name for future lookup
			classObject.__df_className = fullClassName;
			
			// Make cache property unenumerable
			_global.ASSetPropFlags(classObject, "__df_className", 1, true);
		}
		
		// Return full package or just class name as appropriate
		return full ? fullClassName : fullClassName.split(".").pop();
	}
	
	private static function searchForClass (classObject:Object, node:Object, name:String, scanned:Array) {
		var prefix:String = (name == undefined) ? "" : (name + ".");
		
		if (scanned == undefined) {
			scanned = new Array();
		}
		
		scanned.push(node);
	
		for (var childNode:String in node) {
			if (node[childNode] instanceof Function && node[childNode].constructor != null && classObject.valueOf() == node[childNode].valueOf()) {
				return prefix + childNode;
			
			} else if (node[childNode] instanceof Object) {
				var alreadyScanned:Boolean = false;
				for (var i:String in scanned) {
					if (scanned[i].valueOf() == node[childNode].valueOf()) {
						alreadyScanned = true;
						break;
					}
				}
				
				if (!alreadyScanned) {
					var className:String = searchForClass(classObject, node[childNode], prefix + childNode, scanned);
					if (className.length > 0) {
						return className;
					}
				}
			}
		}
	}
}